package cn.ffcs.security.web;

import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import cn.ffcs.base.web.BaseAction;
import cn.ffcs.security.model.Role;

/**
 * 
 * 角色 action层，进行业务逻辑操作<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author liuzhh
 * @version $Id$
 */
public class RoleAction<R extends Role> extends BaseAction<R>
{

    /** 网络标识  */
    private static final long serialVersionUID = -1528148398297298281L;

    /** 业务POJO **/
    private R role;
    /** 存在默认角色 **/
    private String existDef;
    
    /**
     * 保存操作
     * {@inheritDoc}
     */
    public String saveOrUpdate()
    {
        baseService.saveOrUpdate(role);
        return SUCCESS;
    }
    
    /**
     * 删除操作
     * @return
     */
    public String remove()
    {
        baseService.remove(role);
        return SUCCESS;
    }
    
    /**
     * 批量删除操作
     * @return
     */
    public String batchDel()
    {
        String[] ids = getContextObjects("ids");
        baseService.batchDel(ids);
        ;
        return SUCCESS;
    }
    public Criterion combineCond(){
        Criterion cond = null;
        if (super.getConds() != null)
        {
            //遍历参数表
            Iterator i = super.getConds().entrySet().iterator();
            while (i.hasNext())
            {
                Entry entry = (Entry)i.next();
                String key=(String)entry.getKey();
                if (super.getConds().get(key) != null
                        && !StringUtils.isEmpty(super.getConds().get(key).toString()))
                {
                    //根据实际情况做处理
                    Criterion temp=null;
                    if(key.equalsIgnoreCase("name")){
                    	temp=Restrictions.like(key, "%"+super.getConds().get(key).toString()+"%");
                    }else{
                        temp=Restrictions.eq(key, super.getConds().get(key));                        
                    }
                    if (cond == null)
                    {
                        cond = temp;
                    }
                    else
                    {
                        cond = Restrictions.and(cond, temp);
                    }
                }
            }
        }
        
        return cond;
    } 
    /**
     * 根据业务ID获取业务对象
     * @return
     */
    public String findById()
    {
        int roleId=0;
        if (role != null && role.getId() != 0)
        {
            role = (R) baseService.findById(role.getId());
            roleId=role.getId();
        }
        //是否已有默认角色
        Criterion cond = Restrictions.eq("isDefault",1);
        List<R> roles=baseService.findBy(cond);
        if (roles.size()>0){
            int tmpRoleID=roles.get(0).getId();
            //已选择的id与本id不一致是设置
            if (tmpRoleID!=roleId){
                this.setExistDef("Y");
            }
        }

        return SUCCESS;
    }
     
    /**
     * 获取角色业务对象
     * @return
     */
    public R getRole()
    {
        return role;
    }

    /**
     * 设置角色业务对象
     */
    public void setRole(R role)
    {
        this.role = role;
    }

    /**
     * @return the existDef
     */
    public String getExistDef()
    {
        return existDef;
    }

    /**
     * @param existDef the existDef to set
     */
    public void setExistDef(String existDef)
    {
        this.existDef = existDef;
    }

}
